﻿using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System;
using System.IO;
using System.Reflection;

namespace WinUITest.Pages
{
    [Menu("获取根目录")]
    public sealed partial class RootPathPage : Page
    {
        public RootPathPage() => InitializeComponent();
        private void Page_Loaded(object sender, RoutedEventArgs e) => SetRootPath();
        private void BtnSetCurrentDirectory_Click(object sender, RoutedEventArgs e)
        {
            string? path = Path.GetDirectoryName(GetType().Assembly.Location);
            if (string.IsNullOrWhiteSpace(path)) return;
            Directory.SetCurrentDirectory(path);
            SetRootPath();
        }
        private void Load_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Assembly.Load("WinUITest");
                ErrorMessage.Text = "Load:加载成功";
            }
            catch (Exception ex)
            {
                ErrorMessage.Text = ex.Message;
            }
        }
        private void LoadFrom_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Assembly.LoadFrom("WinUITest.dll");
                ErrorMessage.Text = "LoadFrom:加载成功";
            }
            catch (Exception ex)
            {
                ErrorMessage.Text = ex.Message;
            }
        }
        private void LoadFile_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Assembly.LoadFile(GetType().Assembly.Location);
                ErrorMessage.Text = "LoadFile:加载成功";
            }
            catch (Exception ex)
            {
                ErrorMessage.Text = $"LoadFile:{ex.Message}";
            }
        }
        private void SetRootPath()
        {
            Value1.Text = Directory.GetCurrentDirectory();
            Value2.Text = AppDomain.CurrentDomain.BaseDirectory;
            Value3.Text = GetType().Assembly.Location;
        }
    }
}
